#ifndef CUSTOM_GBUFFERS_INCLUDED
#define CUSTOM_GBUFFERS_INCLUDED

TEXTURE2D(_GBuffer0); SAMPLER(sampler_GBuffer0);
TEXTURE2D(_GBuffer1); SAMPLER(sampler_GBuffer1);
TEXTURE2D(_GBuffer2); SAMPLER(sampler_GBuffer2);
TEXTURE2D(_GBuffer3); SAMPLER(sampler_GBuffer3);
TEXTURE2D(_GBuffer4); SAMPLER(sampler_GBuffer4);
TEXTURE2D(_GBufferDepth); SAMPLER(sampler_GBufferDepth);

float4x4 MatrixP;
float4x4 invMatrixVP;
float4x4 MatrixV;

float GetDepth(float2 uv) {
	float depth = SAMPLE_TEXTURE2D(_GBufferDepth, sampler_GBufferDepth, uv).r;
	return depth;
}
float3 GetAlbedo(float2 uv) {
	//float3 worldPosition = SAMPLE_TEXTURE2D(_GBuffer0, sampler_GBuffer0, uv).rgb;
	//float4 positionScreen = float4(worldPosition, 1.0);
	//positionScreen = mul(MatrixV, float4(worldPosition, 1.0));
	//positionScreen = mul(MatrixP, positionScreen);
	//positionScreen /= positionScreen.w;
	////positionScreen = mul(invMatrixVP, positionScreen);
	//positionScreen /= positionScreen.w; 

	//return worldPosition.xyz;

	float3 color = SAMPLE_TEXTURE2D(_GBuffer0, sampler_GBuffer0, uv).rgb;
	return color;
}
float3 GetNormal(float2 uv) {
	float3 packedNormal = SAMPLE_TEXTURE2D(_GBuffer1, sampler_GBuffer1, uv).rgb;
	return (packedNormal - 0.5) * 2;
}
float2 GetLightMap(float2 uv) {
	return SAMPLE_TEXTURE2D(_GBuffer2, sampler_GBuffer2, uv).rg;
}
float3 GetPosition(float2 uv) {
	float depth = GetDepth(uv);
	//float z = -
	//	(MatrixP[3][2]) /
	//	(depth - MatrixP[2][2]);
	float4 screenCoord = float4(2 * (uv - 0.5), 1 - depth * 2, 1);
	float4 position = mul(invMatrixVP, screenCoord); position /= position.w;

	return position.xyz;
	//return abs(position.xyz- GetAlbedo(uv));
}
float3 GetEmission(float2 uv) {
	return SAMPLE_TEXTURE2D(_GBuffer3, sampler_GBuffer3, uv).rgb;
}

#endif